﻿using System.Globalization;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.Mvc.Html;
using So.ImageResizer.Helpers;
using So.ImageResizer.Services;

namespace So.ImageResizer.HtmlHelpers {
    public static class HtmlImageResizeHelper {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="url"></param>
        /// <param name="alt"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="maxWidth"></param>
        /// <param name="maxheight"></param>
        /// <param name="crop">None,Auto,Custom</param>
        /// <param name="scale">DownscaleOnly,UpscaleOnly,Both,UpscaleCanvas</param>
        /// <param name="stretch">Proportionally,Fill</param>
        /// <returns></returns>
        public static MvcHtmlString ResizedImage(this HtmlHelper htmlHelper, string url, string alt, object htmlAttributes, int width = 0,
                                                 int height = 0, int maxWidth = 0, int maxheight = 0,
                                                 ResizeSettingType.CropMode crop = ResizeSettingType.CropMode.Auto, ResizeSettingType.ScaleMode scale = ResizeSettingType.ScaleMode.Both,
                                                 ResizeSettingType.StretchMode stretch = ResizeSettingType.StretchMode.Fill) {
            if (url != null) {
                var queryString = new QueryString()
                    .Add("url", url)
                    .Add("width", width.ToString(CultureInfo.InvariantCulture))
                    .Add("height", height.ToString(CultureInfo.InvariantCulture))
                    .Add("maxWidth", maxWidth.ToString(CultureInfo.InvariantCulture))
                    .Add("maxheight", maxheight.ToString(CultureInfo.InvariantCulture))
                    .Add("cropMode", crop.ToString())
                    .Add("scale", scale.ToString())
                    .Add("stretchMode", stretch.ToString());
                return htmlHelper.Image("/resizedImage" + queryString, alt, new RouteValueDictionary(htmlAttributes));
            }
            return null;
        }
    }
}